using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Auxiliaries;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Form used to create new instances of advanced AEUC controls
    /// </summary>
    public partial class AEUCCreateSettings : Form
    {
        AEUCTabbedParentControl ControlParent;
        /// <summary>
        /// Public constructor of the form for creating advanced AEUC controls
        /// </summary>
        /// <param name="iControlParent">The parent control that will accept the newly created control</param>
        public AEUCCreateSettings(AEUCTabbedParentControl iControlParent)
        {
            ControlParent = iControlParent;

            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            bool faulty_input = false;

            bool is_aux = chbAuxControl.Checked;
            bool state_sense = chbStateSense.Checked;
            bool is_enabled = chbControlEnabled.Checked;

            uint id;
            uint size;
            Int64 val;
            Int64 def_val;
            Int64 min_val;
            Int64 max_val;

            if (!uint.TryParse(tbID.Text, out id))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbID.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct id value.");
                else
                    id = (uint)tmpVal;
            }
            if (!uint.TryParse(tbSize.Text, out size))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbSize.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    size = (uint)tmpVal;
            }
            if (!Int64.TryParse(tbVal.Text, out val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbDefVal.Text, out def_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbDefVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    def_val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbMinVal.Text, out min_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbMinVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    min_val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbMaxVal.Text, out max_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbMaxVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    max_val = (Int64)tmpVal;
            }

            if (!faulty_input)
            {
                Control rdyControl = null;
                if(rdTypeCheckBox.Checked)
                    rdyControl = new Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control.AEUCSettingCheckBox(id,size,val,def_val,min_val,max_val,is_enabled,state_sense,is_aux,tbDisplayName.Text);
                else if(rdTypeComboBox.Checked)
                    rdyControl = new Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control.AEUCComboBox(id, size, val, def_val, min_val, max_val, is_enabled, state_sense, is_aux, tbDisplayName.Text);
                else if (rdTypeNUD.Checked)
                {
                    if (chbPointerField.Checked)
                        rdyControl = new Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control.AEUCNUD(id, size, val, def_val, min_val, max_val, is_enabled, state_sense, is_aux, tbDisplayName.Text);
                    else
                        rdyControl = new Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control.AEUCNUD(id, size, val, def_val, min_val, max_val, is_enabled, state_sense, is_aux, tbDisplayName.Text);
                }
                rdyControl.Location = ControlParent.determineControlPosition(rdyControl);

                ControlParent.TabPages[ControlParent.SelectedIndex].Controls.Add(rdyControl);
                
                Close();
            }
        }

        private void rdTypeNUD_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTypeNUD.Checked)
            {
                chbPointerField.Enabled = true;
            }
            else
            {
                chbPointerField.Checked = chbPointerField.Enabled = false;
            }
        }

        private void chbPointerField_CheckedChanged(object sender, EventArgs e)
        {
            if (chbPointerField.Checked)
            {
                tbSize.Text = "4";
                tbVal.Text = "0";
                tbMinVal.Text = "0";
                tbMaxVal.Text = "0xFFFFFFFF";
                tbDefVal.Text = "0";
                tbSize.Enabled = tbVal.Enabled = tbMinVal.Enabled = tbMaxVal.Enabled = tbDefVal.Enabled = false;
            }
            else
            {
                tbSize.Enabled = tbVal.Enabled = tbMinVal.Enabled = tbMaxVal.Enabled = tbDefVal.Enabled = true;
            }
        }
    }
}